package com.example.runningman.run;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.example.runningman.MainActivity;
import com.example.runningman.R;
import com.example.runningman.adapter.Bushu_adapter;
import com.example.runningman.adapter.Run_adapter;
import com.example.runningman.bean.Steps;
import com.example.runningman.dao.StepsDao;
import com.example.runningman.database.BaiduPointsDatebase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class PointActivity extends AppCompatActivity {
    RecyclerView mRecyclerView;
    ImageButton imageButton;
    Bushu_adapter bushu_adapter;
    StepsDao stepsDao;
    BaiduPointsDatebase baiduPointsDatebase ;
    LinearLayoutManager layoutManager;
    List<Steps> list=new ArrayList<>();
    List<Steps> lists=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_point);
        mRecyclerView = findViewById(R.id.bushu);
        baiduPointsDatebase = BaiduPointsDatebase.getInstance(this);
        stepsDao=baiduPointsDatebase.StepsDao();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        Calendar c = Calendar.getInstance();
        list=stepsDao.selectAllSteps();
        if(list.size()>1){
            if(list.size()<30){
                for(int i = 1 ; i<list.size() ; i++){
                    if(list.get(list.size()-i).getTime()!=0&&list.get(list.size()-i).getStep()!=0){
                        Steps steps = new Steps();
                        Date date = new Date(list.get(list.size()-i).getTime());
                        c.setTime(date);
                        c.add(Calendar.DAY_OF_MONTH, -1);
                        Date yesterday = c.getTime();//这是昨天
                        long tmp=yesterday.getTime();
                        steps.setTime(tmp);
                        if(list.get(list.size()-i).getStep()>list.get(list.size()-i-1).getStep()){
                            steps.setStep(list.get(list.size()-i).getStep()-list.get(list.size()-i-1).getStep());
                        }else {
                            steps.setStep(list.get(list.size()-i).getStep());
                        }
                        lists.add(steps);
                    }

                }
            }else {
                for(int i = 1 ; i<30 ; i++){
                    if(list.get(list.size()-i).getTime()!=0&&list.get(list.size()-i).getStep()!=0){
                        Steps steps = new Steps();
                        Date date = new Date(list.get(list.size()-i).getTime());
                        c.setTime(date);
                        c.add(Calendar.DAY_OF_MONTH, -1);
                        Date yesterday = c.getTime();//这是昨天
                        long tmp=yesterday.getTime();
                        steps.setTime(tmp);
                        if(list.get(list.size()-i).getStep()>list.get(list.size()-i-1).getStep()){
                            steps.setStep(list.get(list.size()-i).getStep()-list.get(list.size()-i-1).getStep());
                        }else {
                            steps.setStep(list.get(list.size()-i).getStep());
                        }
                        lists.add(steps);
                    }

                }
            }
        }



        bushu_adapter = new Bushu_adapter(lists, PointActivity.this);
        layoutManager = new LinearLayoutManager(PointActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(bushu_adapter);
        imageButton=findViewById(R.id.back3);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PointActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}
